//
//  QReceiveOrderVC.m
//  QoilPublic
//
//  Created by 杨凯 on 16/7/5.
//  Copyright © 2016年 杨凯. All rights reserved.
//

#import "QReceiveOrderVC.h"
#import "QAllOrderCell.h"
#import "QAllorderModel.h"
#import "QAllorderListModel.h"
#import "QDetailsOrderVC.h"
#import "QLoginVC.h"
@interface QReceiveOrderVC ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation QReceiveOrderVC{

    UITableView *_tableView;
    
    NSInteger _index;
    NSMutableArray *_dataArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];//接单

    self.view.backgroundColor = [QColorUtility defaultBackGroundColor];
    _dataArr = [NSMutableArray array];

    [self createTableView];
    [self requestData];

    _tableView.mj_header = [QViewUtility showHeadRefresh:^{
        [_tableView.mj_footer resetNoMoreData];
        [self requestData];
    }];
    _tableView.mj_footer = [QViewUtility showFootRefresh:^{
        [self requestMoreData];
    }];
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestData) name:kNotificationTabBar2 object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notifiAction:) name:kNotificationscrollsToTop object:nil];
    
    //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestData) name:kNotificationDetailPush object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestData) name:@"orderOrder" object:nil];
    
}

- (void)notifiAction:(NSNotification *)notification{
    NSString *topStr = [notification.userInfo objectForKey:kTopKey];
    if([topStr isEqualToString:@"1"]){
        _tableView.scrollsToTop = YES;
    }else{
        _tableView.scrollsToTop = NO;
    }
}

#pragma mark --- createTableView
- (void)createTableView{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-45-(kNavHeight+kStatusHeight)) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    
    [_tableView registerNib:[UINib nibWithNibName:@"QAllOrderCell" bundle:nil] forCellReuseIdentifier:@"QAllOrderCell"];
}

#pragma mark --- tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return _dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    QAllOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QAllOrderCell" forIndexPath:indexPath];
    cell.indexPath = indexPath;
    if(_dataArr.count){
        cell.model = _dataArr[indexPath.section];
        cell.orderStyle = OrderReceiveSyle;//可取消的
    }
    __block QReceiveOrderVC *wself = self;
    cell.StyleBtnBlock = ^(NSInteger btnTag,NSInteger cellSection){
        
        [QViewUtility showAlertChooseViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"确定取消订单?" leftTitle:@"取消" rightTitle:@"确定" type:1 myBlock:^(NSInteger tag) {
            if(tag == 1){
                [wself cancelOrderRequestData:_dataArr[cellSection]];

            }
        }];

    };

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 180;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    __block QReceiveOrderVC *wself = self;
    QAllorderListModel *model = _dataArr[indexPath.section];
    QDetailsOrderVC *vc = [[QDetailsOrderVC alloc] init];
    vc.detailOrderStyle = DetailsOrderwaitedStyle;
    vc.model = model;
    vc.orderId = model.orderId;
    vc.pushStyle = DetailsPushFrontStyle;

       vc.cancelBlock = ^(){
        [wself requestData];
    };
    
    [self.navigationController pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return [[UIView alloc]init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    return [[UIView alloc]init];
}
//加载第一页
- (void)requestData{
    _index = 1;
    NSString *star = [NSString stringWithFormat:@"%ld",(long)_index];
    NSString *url = [QParamService urlWithAction:@"/order/userList"];
    NSDictionary *param = [QParamService allMyorderType:@"1" start:star pageSize:KpageSize];
    [QViewUtility showMBHudToView:self.view hideBg:YES];
    [QViewUtility dismissNoDataViewInVC:self];
    [QViewUtility disMissNoNetWorkInVC:self];
    [[QHttpManager sharedManager] POST:url parameterMD5:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [_tableView.mj_header endRefreshing];
        [QViewUtility hideMBHudToView:self.view];
        if(error){
            if([QUserInfo sharedInfo].isHaveNetWork == NO){
                [QViewUtility showNoNetWorkInView:_tableView];
            }
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            QAllorderModel *model = [QAllorderModel objectFromDict:basModel.items];
            [_dataArr removeAllObjects];
            [_dataArr addObjectsFromArray:model.list];
            if(_dataArr.count == 0){
                [QViewUtility showNoDataViewInView:_tableView string:@""];
            }else{
                if(model.list.count < 10){
                }
            }
            [_tableView reloadData];
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }];
}

//加载更多
- (void)requestMoreData{
    _index++;
    NSString *star = [NSString stringWithFormat:@"%ld",(long)_index];
    NSString *url = [QParamService urlWithAction:@"/order/userList"];
    NSDictionary *param = [QParamService allMyorderType:@"1" start:star pageSize:KpageSize];
    [[QHttpManager sharedManager] POST:url parameterMD5:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [_tableView.mj_footer endRefreshing];
        if(error){
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        
        if([basModel.result isEqualToString:@"success"]){
            QAllorderModel *model = [QAllorderModel objectFromDict:basModel.items];
            [_dataArr addObjectsFromArray:model.list];
            if(model.list.count < 10){
                [_tableView.mj_footer noticeNoMoreData];
            }
            [_tableView reloadData];
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }];
}

#pragma mark ---取消订单
- (void)cancelOrderRequestData:(QAllorderListModel *)model{
    
    __block QReceiveOrderVC *wself = self;
    NSString *url = [QParamService urlWithAction:@"/order/cancel"];
    NSDictionary *param = [QParamService cancelOrder:model.orderId];
    [QViewUtility showMBHudToView:self.view hideBg:NO];
    [[QHttpManager sharedManager] POST:url parameterMD5:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [QViewUtility hideMBHudToView:self.view];
        if(error){
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
//           [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"取消成功" btnTitle:showAlertBtnTitle type:1 myBlock:^{
//               [wself requestData];
//           }];
            //[[NSNotificationCenter defaultCenter] postNotificationName:kNotificationRegisterSuccess object:nil];
            [wself requestData];

        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }];
}


@end
